package com.yunwushanhe.student.bean.ret;

import com.yunwushanhe.student.Contants.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Boolean success = Boolean.TRUE;
    private Integer code = CodeEnum.SUCCESS.getCode();
    private String msg = CodeEnum.SUCCESS.getMassage();
    private T data;
    private Long total;

    public static <T> Result<T> ok(){
        return new Result<>();
    }

    public static <T> Result<T> ok(T data){
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok(T data,Long total){
        Result<T> result = new Result<>();
        result.setData(data);
        result.setTotal(total);
        return result;
    }

    public static <T> Result<T> fail(String message){
        Result<T> result = new Result<>();
        result.setCode(null);
        result.setSuccess(false);
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> fail(Integer code,String message){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(false);
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> fail(CodeEnum codeEnum){
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setCode(codeEnum.getCode());
        result.setMsg(codeEnum.getMassage());
        return result;
    }
}
